package com.app.controller;


import com.app.common.SkilledTool;
import com.app.model.CusResumeLangModel;
import com.app.model.CustomerModel;
import com.app.model.LanguageModel;
import com.app.model.UserModel;
import com.app.service.CusResumeLangskillService;
import com.app.service.CustomerService;
import com.app.service.LanguageService;
import com.app.service.UserService;
import com.app.util.Result;
import com.app.util.TokenUtils;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.List;

@Controller
@RestController
public class CusResumeLangController {

    @Autowired
    public CusResumeLangskillService cusResumeLangskillService;

    @Autowired
    private CustomerService customerService;

    @Autowired
    UserService userService;
    @Autowired
    LanguageService languageService;
    @GetMapping("/findAllByCustomerId")
    public Result<Object> findAllByPid(HttpServletRequest request){

        String id = request.getParameter("id");
        String token = request.getHeader("token");

        CustomerModel cus = null;
        if(id!=null&&!"0".equals(id)&&!"undefined".equals(id))
        {
            cus = customerService.getCustomerById(Integer.valueOf(id));
        }else {
            String mobile = TokenUtils.nothingness(token);
            if (mobile == null) {
                return Result.error(405,"token不存在");
            }

            UserModel user = userService.getUserByMobile(mobile);
            if(user==null)
            {
                return Result.error(405,"用户不存在");
            }
            cus = customerService.getCustomerById(user.getCId());
        }
        List<CusResumeLangModel> lang = cusResumeLangskillService.findByCustomerId(cus.getId());
        if(lang!=null)
        {
            for (int i = 0; i < lang.size(); i++) {
                for (int j = 0; j < SkilledTool.SKILL_VO_LIST.size(); j++) {
                    if(SkilledTool.SKILL_VO_LIST.get(j).getId()==lang.get(i).getRedAndWrt())
                    {
                        lang.get(i).setRedAndWrtText(SkilledTool.SKILL_VO_LIST.get(j).getData());
                        break;
                    }
                }
                LanguageModel model = languageService.getModelById(lang.get(i).getLanguages());
                lang.get(i).setLanguagesText(model.getLanguages());
            }
        }

        return Result.success(lang);

    }
    @RequestMapping(value = "/deleteLangskillById",method = RequestMethod.GET)
      public Result<Object> deleteById(int id){
        try {
            if(id>0)
            {
                cusResumeLangskillService.delete(id);
            }else {
               return Result.error(500,"ID不存在");
            }
        }catch (Exception e)
        {
            System.out.println(e);
        }
       return Result.success("操作成功");
      }
      //增/改
    @PostMapping("/addOrupLangskill")
    public Result<Object> addAndUpdate(@RequestParam("id") int id,
                               @RequestParam("languages") Integer languages,
                               @RequestParam("redAndWrt") Integer redAndWrt,
                               @RequestParam("cert") String cert,
                               HttpServletRequest request
    ){
        JSONObject json = new JSONObject();
        String token = request.getHeader("token");
        String mobile = TokenUtils.nothingness(token);
        if (mobile == null) {
            return Result.error(405,"token不存在");
        }

        UserModel user = userService.getUserByMobile(mobile);
        if(user==null)
        {
            return Result.error(405,"用户不存在");
        }
        CustomerModel cus = customerService.getCustomerById(user.getCId());
        CusResumeLangModel cusResumeLang = new CusResumeLangModel();
        cusResumeLang.setLanguages(languages);
        cusResumeLang.setRedAndWrt(redAndWrt);
        cusResumeLang.setCert(cert);
        cusResumeLang.setCustomerId(cus.getId());
        try{
            if (id>0){
                cusResumeLang.setId(id);
                cusResumeLangskillService.update(cusResumeLang);
            }else{
                cusResumeLangskillService.insert(cusResumeLang);
            }
        }catch (Exception e)
        {
            System.out.println(e);
        }
       return Result.success(null);
    }
    //根据ID语言能力
    @GetMapping("/getLangById")
    public Result<Object> getLangById(HttpServletRequest request) throws ParseException {
        String id = request.getParameter("id");
        CusResumeLangModel model = cusResumeLangskillService.findLangById(Integer.valueOf(id));
        return Result.success(model);
    }

    @GetMapping("/getLanguageList")
    public Result<Object> getLanguageList(HttpServletRequest request) throws ParseException {
        return Result.success(languageService.getAllList());
    }

    @GetMapping("/getSkilledList")
    public Result<Object> getSkilledList(HttpServletRequest request) throws ParseException {
        return Result.success(SkilledTool.SKILL_VO_LIST);
    }
}
